{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "20619726",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "决策树准确率: 0.9573333333333334\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.98      0.96      0.97      2308\n",
      "           1       0.88      0.94      0.91       692\n",
      "\n",
      "    accuracy                           0.96      3000\n",
      "   macro avg       0.93      0.95      0.94      3000\n",
      "weighted avg       0.96      0.96      0.96      3000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据读取与预处理\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第5章 决策树模型\\源代码汇总_PyCharm格式\\员工离职预测模型.xlsx\")\n",
    "df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})\n",
    "\n",
    "# 2. 划分特征和目标变量\n",
    "X = df.drop(columns='离职')\n",
    "y = df['离职']\n",
    "\n",
    "# 3. 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "\n",
    "# 4. 决策树模型训练\n",
    "dt_model = DecisionTreeClassifier(max_depth=3, random_state=123)\n",
    "dt_model.fit(X_train, y_train)\n",
    "\n",
    "# 5. 预测和评估\n",
    "y_pred = dt_model.predict(X_test)\n",
    "print(\"决策树准确率:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9b33099e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林准确率: 0.994\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.99      1.00      1.00      2308\n",
      "           1       1.00      0.98      0.99       692\n",
      "\n",
      "    accuracy                           0.99      3000\n",
      "   macro avg       1.00      0.99      0.99      3000\n",
      "weighted avg       0.99      0.99      0.99      3000\n",
      "\n",
      "\n",
      "特征重要性:\n",
      "  feature  importance\n",
      "1     满意度    0.351536\n",
      "5      工龄    0.185623\n",
      "3    工程数量    0.181493\n",
      "4     月工时    0.146782\n",
      "2    考核得分    0.125931\n",
      "0      工资    0.008635\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据读取与预处理\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第5章 决策树模型\\源代码汇总_PyCharm格式\\员工离职预测模型.xlsx\")\n",
    "df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})\n",
    "\n",
    "# 2. 划分特征和目标变量\n",
    "X = df.drop(columns='离职')\n",
    "y = df['离职']\n",
    "\n",
    "# 3. 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "\n",
    "# 4. 随机森林模型训练\n",
    "rf_model = RandomForestClassifier(n_estimators=100, random_state=123)\n",
    "rf_model.fit(X_train, y_train)\n",
    "\n",
    "# 5. 预测和评估\n",
    "y_pred = rf_model.predict(X_test)\n",
    "print(\"随机森林准确率:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# 特征重要性\n",
    "feature_importance = pd.DataFrame({\n",
    "    'feature': X.columns,\n",
    "    'importance': rf_model.feature_importances_\n",
    "}).sort_values('importance', ascending=False)\n",
    "print(\"\\n特征重要性:\")\n",
    "print(feature_importance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1bb05967",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑回归准确率: 0.7823333333333333\n",
      "\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.94      0.87      2308\n",
      "           1       0.56      0.27      0.37       692\n",
      "\n",
      "    accuracy                           0.78      3000\n",
      "   macro avg       0.68      0.60      0.62      3000\n",
      "weighted avg       0.75      0.78      0.75      3000\n",
      "\n",
      "\n",
      "前5个样本的预测概率:\n",
      "      不离职概率      离职概率\n",
      "0  0.927798  0.072202\n",
      "1  0.940731  0.059269\n",
      "2  0.850882  0.149118\n",
      "3  0.910709  0.089291\n",
      "4  0.429343  0.570657\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 数据读取与预处理\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第5章 决策树模型\\源代码汇总_PyCharm格式\\员工离职预测模型.xlsx\")\n",
    "df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})\n",
    "\n",
    "# 2. 划分特征和目标变量\n",
    "X = df.drop(columns='离职')\n",
    "y = df['离职']\n",
    "\n",
    "# 3. 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "\n",
    "# 4. 数据标准化（逻辑回归需要）\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "# 5. 逻辑回归模型训练\n",
    "lr_model = LogisticRegression(random_state=123)\n",
    "lr_model.fit(X_train_scaled, y_train)\n",
    "\n",
    "# 6. 预测和评估\n",
    "y_pred = lr_model.predict(X_test_scaled)\n",
    "print(\"逻辑回归准确率:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\n分类报告:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# 预测概率\n",
    "y_pred_proba = lr_model.predict_proba(X_test_scaled)\n",
    "print(\"\\n前5个样本的预测概率:\")\n",
    "print(pd.DataFrame(y_pred_proba, columns=['不离职概率', '离职概率']).head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed58d09c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
